package com.demo.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.demo.model.MenuInfo;

public class MenuDao extends HibernateDaoSupport {

	protected final Log log = LogFactory.getLog(getClass());
	private Class persistentClass;

	public MenuDao() {
		super();
		this.persistentClass = MenuInfo.class;
	}

	@SuppressWarnings("unchecked")
	public MenuInfo get(int id) {
		MenuInfo entity = (MenuInfo) super.getHibernateTemplate().get(
				persistentClass, id);
		if (entity == null) {
			log.warn("Uh oh, '" + persistentClass + "' object with id '" + id
					+ "' not found...");
			throw new ObjectRetrievalFailureException(persistentClass, id);
		}
		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<MenuInfo> getSubMenu(int superId) {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(this.persistentClass);

		criteria.add(Restrictions.eq("superId", superId));

		criteria.addOrder(Order.asc("id"));

		return super.getHibernateTemplate().findByCriteria(criteria);
	}

}
